package com.lzw.mall.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.lzw.mall.common.entity.Member;
import com.lzw.mall.common.exception.BusinessException;
import com.lzw.mall.common.mapper.MemberMapper;
import com.lzw.mall.common.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzw.mall.common.util.RequestUtil;
import com.lzw.mall.common.util.StringUtil;
import com.lzw.mall.common.vo.LayuiTableVO;
import com.lzw.mall.common.vo.OrderSessionUtil;
import com.lzw.mall.common.vo.ResultVO;
import com.ramostear.captcha.HappyCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lizhenwei
 * @since 2023-03-23
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public LayuiTableVO list(Integer page, Integer limit, String keyword, String sex, String status) {
        PageHelper.startPage(page,limit);
        return LayuiTableVO.data(memberMapper.selectListPage(keyword,sex,status));
    }

    @Override
    public ResultVO doSave(Member u) {
        ResultVO vo=new ResultVO();
        if (u.getId() == null) {//新增
            if(!StringUtil.isNotBlank(u.getUsername())){
                throw new BusinessException("用户名不能为空！") ;
            }
            if(!StringUtil.isNotBlank(u.getPassword())){
                throw new BusinessException("密码不能为空") ;
            }
            Member user =memberMapper.selectByUsername(u.getUsername());
            if(user!=null){
                throw new BusinessException("用户名已经被使用 ") ;
            }
            u.setPassword(StringUtil.md5(u.getPassword()));
            vo.setMsg("新增用户成功");
            memberMapper.insert(u);
            return vo;
        } else {
                List<Member> list =memberMapper.selectByUsernameAndId(u.getUsername(),u.getId());
                if(list.size()>0){
                    throw new BusinessException("用户名已经被使用 ") ;
                }
                memberMapper.doUpdateById(u.getId(),u.getUsername(),u.getSex(),u.getName(),u.getPhone(),u.getAvatar());
                vo.setMsg("编辑成功");
                return vo;
        }
    }

    @Override
    public ResultVO doSelectById(Integer id) {
        ResultVO vo=new ResultVO();
        vo.setCode(0);
        vo.setData(memberMapper.doSelectById(id));
        return vo;
    }

    @Override
    public ResultVO updatePassword(String oldPassword, String newPassword, String againPassword) {
        ResultVO vo=new ResultVO();
        Member u=memberMapper.selectByUsername(RequestUtil.getCurrent().getUsername());
        String p=u.getPassword();
        Integer id=u.getId();
        if (p.equals(StringUtil.md5(oldPassword))) {
            if (newPassword.equals(againPassword)){
                memberMapper.updatePassword(StringUtil.md5(newPassword),id);
                vo.setMsg("修改成功，请重新登录！");
            }else {
                throw new BusinessException("输入的两次密码不一致！请重新输入");
            }
        }else {
            throw new BusinessException("原密码输入错误！请重新输入");
        }
        return vo;
    }

    @Override
    public ResultVO updateMima(Integer id) {
        ResultVO vo=new ResultVO();
        memberMapper.updateMima(id);
        vo.setMsg("重置成功！初始密码为123456");
        return vo;
    }

    @Override
    public void openMember(Integer[] ids) {
        memberMapper.openMember(ids);
    }

    @Override
    public void closeMember(Integer[] ids) {
        memberMapper.closeMember(ids);
    }

    @Override
    public boolean check(String username, String password, String code, HttpServletRequest request) {
        System.out.println(username);
        System.out.println(password);
        boolean flag = HappyCaptcha.verification(request,code,true);
        if (flag) {
            Member member = memberMapper.selectByUsername(username);
            boolean isUsernameCorrect = member != null && member.getPassword().equals(StringUtil.md5(password));

            Member memberByPhone = memberMapper.selectByPhone(username);
            boolean isPhoneNumberCorrect = memberByPhone != null && memberByPhone.getPassword().equals(StringUtil.md5(password));

            if (isUsernameCorrect || isPhoneNumberCorrect) {
                // 用户名或电话号码以及对应密码都正确
                if (member == null){
                    OrderSessionUtil.setCurrent(memberByPhone);
                }else {
                    OrderSessionUtil.setCurrent(member);
                }
                return true;
            } else {
                // 用户名或电话号码或密码输入不正确
                throw new BusinessException("用户名或密码输入错误请重新输入");
            }
        } else {
            // 验证码输入错误
            throw new BusinessException("验证码输入错误请重新输入");
        }
    }

    @Override
    public boolean selectByPhone(String phone) {
        Member member = memberMapper.selectByPhone(phone);
        return member != null;
    }

    @Override
    public boolean selectByUsername(String username) {
        Member m =memberMapper.selectByUsername(username);
        return m != null;
    }

    @Override
    public void doInsert(Member m) {
        baseMapper.insert(m);
    }

    @Override
    public ResultVO doSelectByUsername() {
        ResultVO vo=new ResultVO();
        String username = null;
        Member orderSession = OrderSessionUtil.getCurrent();
        if (orderSession != null) {
            username = orderSession.getUsername();
            Member member = memberMapper.selectByUsername(username);
            if (member == null) {
                member = memberMapper.selectByPhone(username);
            } else {
                vo.setData(member);
                // 其他逻辑处理
            }
        }
//        String username=OrderSessionUtil.getCurrent().getUsername();
        return vo;
    }
}
